package com.leave.mos.controller.simple;

import com.leave.mos.constant.TablenameConstant;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @ClassName ComparisonQueryOperatorsController
 * @Description TODO
 * @Author lh
 * @Date 2021/7/12 16:50
 * @Version 1.0
 *
 * Evaluation Query Operators 评估查询运算符
 *
 **/
@RestController
@RequestMapping("evaluation")
public class EvaluationQueryOperatorsController {

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     *
     * @TODO 允许在查询语言中使用聚合表达式。
     */
    @RequestMapping("expr")
    public List<Map> expr(){
        //获取 订单金额大于1000 的 订单
        Criteria c = new Criteria().andOperator(
                //Criteria.where(TablenameConstant.goods_order.status).gt("$userId")
                //Expression
        );
        Query query = new Query();
        query.addCriteria(c);

        List<Map> maps = mongoTemplate.find(query, Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    /**
     * @TODO jsonSchema
     * @return
     */
    @RequestMapping("jsonSchema")
    public List<Map> jsonSchema(){
        //获取 订单金额大于1000 的 订单
        Criteria c = new Criteria().andOperator(
                //Criteria.where(TablenameConstant.goods_order.status).gt("$userId")
                //Expression
        );
        Query query = new Query();
        query.addCriteria(c);

        List<Map> maps = mongoTemplate.find(query, Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    /**
     * 求模
     * 选择字段值除以除数具有指定余数的文档（即执行模运算以选择文档）。
     * @return
     */
    @RequestMapping("mod")
    public List<Map> mod(){
        //获取 订单金额大于1000 的 订单
        Criteria c = new Criteria().andOperator(
                //除数4 余数0
               Criteria.where(TablenameConstant.goods_order.status).mod(2,1)
        );
        Query query = new Query();
        query.addCriteria(c);

        List<Map> maps = mongoTemplate.find(query, Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    static final Pattern pattern = Pattern.compile("order*");

    /**
     * 正则表达式 regex
     *
     * @return
     */
    @RequestMapping("regex")
    public List<Map> regex(){
        //获取 订单金额大于1000 的 订单

        Criteria c = new Criteria().andOperator(
                //Criteria.where(TablenameConstant.goods_order.orderCode).regex("order*")
                Criteria.where(TablenameConstant.goods_order.orderCode).regex(pattern)
        );
        Query query = new Query();
        query.addCriteria(c);

        List<Map> maps = mongoTemplate.find(query, Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    /**
     * @TODO
     * @return
     */
    @RequestMapping("text")
    public List<Map> text(){

        //
        Criteria criteria1 = new Criteria(TablenameConstant.goods_order.orderName).is("订");
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    /**
     * @TODO AGG
     * @return
     */
    @RequestMapping("text1")
    public List<Map> text1(){

        return null;
    }

    /**
     * @TODO AGG
     * @return
     */
    @RequestMapping("where")
    public List<Map> where(){

        return null;
    }


}
